function uploadDirect(file, progressBar) {
    const formData = new FormData();
    formData.append('files', file);

    axios.post('/admin/upload', formData, {
        onUploadProgress: (e) => updateProgress(e, progressBar)
    })
        .catch((error) => {
            console.error('Upload failed, retrying...', error);
            uploadDirect(file, progressBar); // 递归重试
        });
}

function uploadChunked(file, progressBar) {
    const chunkSize = 100 * 1024; 
    const totalChunks = Math.ceil(file.size / chunkSize);
    let uploaded = 0;

    const uploadChunk = (start, chunkNumber) => {
        return new Promise(resolve => {
            const attemptUpload = () => {
                const chunk = file.slice(start, start + chunkSize);
                const formData = new FormData();
                formData.append('file', chunk);
                formData.append('filename', file.name);
                formData.append('chunkNumber', chunkNumber);
                
                axios.post('/admin/upload-chunk', formData)
                    .then(() => {
                        uploaded++;
                        updateProgress({ 
                            loaded: uploaded, 
                            total: totalChunks 
                        }, progressBar);
                        resolve();
                    })
                    .catch(error => {
                        console.error(`Chunk ${chunkNumber} retrying...`, error);
                    });
            };
            attemptUpload();
        });
    };

    const mergeChunks = () => {
        return new Promise(resolve => {
            const attemptMerge = () => {
                axios.post('/admin/merge-chunks', {
                    filename: file.name,
                    totalChunks: totalChunks
                })
                .then(() => {
                    console.log('Merge successful');
                    resolve();
                })
                .catch(error => {
                    console.error('Merge retrying...', error);
                });
            };
            attemptMerge();
        });
    };

    // 并行上传所有分块 
    const uploadPromises = Array.from({ length: totalChunks }, (_, i) => 
        uploadChunk(i * chunkSize, i)
    );

    Promise.all(uploadPromises)
        .then(mergeChunks)
        .catch(error => console.error('Fatal error:', error));
}